package com.wukong.logisticsproject.service.impl;

import com.wukong.logisticsproject.ex.InsertException;
import com.wukong.logisticsproject.mapper.BranchMapper;
import com.wukong.logisticsproject.mapper.WaybillMapper;
import com.wukong.logisticsproject.model.Branch;
import com.wukong.logisticsproject.model.Problem;
import com.wukong.logisticsproject.mapper.ProblemMapper;
import com.wukong.logisticsproject.model.Waybill;
import com.wukong.logisticsproject.service.IProblemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 问题件表 服务实现类
 * </p>
 *
 * @author wukong.com
 * @since 2020-10-08
 */
@Service
public class ProblemServiceImpl extends ServiceImpl<ProblemMapper, Problem> implements IProblemService {

    @Autowired
    private BranchMapper branchMapper;

    @Autowired
    private WaybillMapper waybillMapper;

    @Autowired
    private ProblemMapper problemMapper;

    @Override
    public Integer addProblem(Problem problem) {

        //添加电话和登记时间
        Branch branch=branchMapper.findBranchByName(problem.getRegisterBranch());
        String Phone=branch.getBranchPhone();

        problem.setPhone(Phone).setRegisterTime(LocalDateTime.now());

        System.out.println("数据为---："+problem);
        Integer row=problemMapper.addProblem(problem);

        //修改运单状态
        waybillMapper.updateWaybillById(new Waybill().setWaybillId(problem.getWaybillId()).setWaybillState("10"));

        if (row!=1){
            throw new InsertException("上传问题件失败！！");
        }

        return row;
    }
}
